package com.test.typeGame.core;

import java.awt.*;
import java.io.*;
import java.util.Vector;

public class TGZhCharObject {
    private String zhCharCiZuFilename = "/dict/zhCharCiZu.txt";
    private String zhCharFilename = "/dict/zhChar500.txt";
    private Font font = new Font("monospace", Font.PLAIN, 30);//TimesRoman
    private Font indexNameFont = new Font("monospace", Font.PLAIN, 15);//TimesRoman
    private Color color = getRandomColorCode();
    private String indexName = "";
    private int index = -1;
    private String data = "";
    private int x = 1;
    private int y = 20;
    private int stepY = 1;
    private int stepYIndex = -1;

    private Vector<String> zhCharDict = new Vector<String>();
    private Vector<String> zhCharCiZuDict = new Vector<String>();

    public TGZhCharObject(){
        initDict();
        initData();
    }

    public void initDict(){
        try {
            InputStream inputStream = this.getClass().getResourceAsStream(zhCharFilename);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null){
                String trimLine = line.trim();
                if (!"".equals(trimLine)){
                    zhCharDict.add(trimLine);
                }
            }

            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

            inputStream = this.getClass().getResourceAsStream(zhCharCiZuFilename);
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            while ((line = bufferedReader.readLine()) != null){
                String trimLine = line.trim();
                if (!"".equals(trimLine)){
                    zhCharCiZuDict.add(trimLine);
                }
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
        }catch (Exception e){
        }
    }

    public Font getFont(){
        return font;
    }

    public Font getIndexNameFont(){
        return indexNameFont;
    }

    public static Color getRandomColorCode(){
        boolean bRandom = false;
        if (bRandom) {
            int r = (int) (Math.random() * 250);
            int b = (int) (Math.random() * 250);
            int g = (int) (Math.random() * 250);
            return new Color(r, b, g);
        }else {
            Color[] colors = {
                    //Color.blue,
                    Color.RED,
                    Color.CYAN,
                    Color.MAGENTA,
                    Color.ORANGE,
                    Color.PINK,
                    Color.YELLOW,
                    new Color(255,185,15)
            };
            int idx = (int) (Math.random() * colors.length);
            return colors[idx];
        }
    }

    void randomY(){
        y = 20;
    }
    void randomX(){
        x = (int)(Math.random()*(800-190*2)+160);
    }

    void setStepYIndex(int stepYIndex){
        this.stepYIndex = stepYIndex;
    }
    void randomStepY(){
        if (-1 == stepYIndex) {
            stepY = 1;
        }else{
            stepY = stepYIndex;
        }
    }
    void randomData(){
        if (this.index == 2) {
            int idx = (int) (Math.random() * zhCharCiZuDict.size());
            data = zhCharCiZuDict.get(idx);
        }else{
            int idx = (int) (Math.random() * zhCharDict.size());
            data = zhCharDict.get(idx);
        }
    }

    void initData(){
        do {
            color = getRandomColorCode();
        }while (Color.black == color);
        randomData();
        randomX();
        randomY();
        randomStepY();
    }

    void nextData(){
        do {
            color = getRandomColorCode();
        }while (Color.black == color);
        randomData();
        randomX();
        randomY();
        randomStepY();
    }

    void nextData(int index){
        setRandomDataIndex(index);
        nextData();
    }

    void nextData(String data){
        nextData();
        this.data = data;
        this.indexName = "";
    }

    //1词，2词组
    void setRandomDataIndex(int index){
        this.index = index;
    }

    void runDown(){
        y += stepY;
    }

    int getY(){
        return y;
    }
    int getX(){
        return x;
    }

    String getIndexName(){
        return indexName;
    }

    Color getCharColor(){
        return color;
    }


    public String getData(){
        return data;
    }

    boolean checkEquel(String data){
        if (data.trim().equals(this.data.trim())){
            return true;
        }else{
            return false;
        }
    }
}
